﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Copy4Backup
{
	/// <summary>
	/// Interaction logic for ProjectWindow.xaml
	/// </summary>
	public partial class ProjectWindow : Window
	{
		#region Fields

		private readonly Project project_;
		
		#endregion Fields

		/// <summary>
		/// Suppress default ctor usage.
		/// </summary>
		private ProjectWindow()
		{
		}

		public ProjectWindow(Project project)
		{
			project_ = project;

			InitializeComponent();
		}

		private void buttonSave_Click(object sender, RoutedEventArgs e)
		{
			//todo: validation

			project_.Name = textBoxName.Text;

			project_.ClearSourceFolders();
			foreach (String sourceFolder in listBoxSourceFolders.Items)
			{
				project_.AddSourceFolder(sourceFolder);
			}

			project_.DestinationFolder = textBoxDestinationFolder.Text;

			project_.ClearFolderFilters();
			foreach (String folderFilter in listBoxFolderFilters.Items)
			{
				project_.AddFolderFilter(folderFilter);
			}

			project_.ClearFileFilters();
			foreach (String fileFilter in listBoxFileFilters.Items)
			{
				project_.AddFileFilter(fileFilter);
			}


			DialogResult = true;
		}

		private void buttonCancel_Click(object sender, RoutedEventArgs e)
		{
			DialogResult = false;
		}

		private void buttonSourceFolderAdd_Click(object sender, RoutedEventArgs e)
		{
			System.Windows.Forms.FolderBrowserDialog browse = new System.Windows.Forms.FolderBrowserDialog();
			browse.ShowNewFolderButton = false;
			browse.Description="Please select a folder";
			if (browse.ShowDialog() == System.Windows.Forms.DialogResult.OK)
			{
				if (!listBoxSourceFolders.Items.Contains(browse.SelectedPath))
				{
					listBoxSourceFolders.Items.Add(browse.SelectedPath);
				}
			}
		}

		private void buttonSourceFolderDelete_Click(object sender, RoutedEventArgs e)
		{
			int index = listBoxSourceFolders.SelectedIndex;

			if (0 <= index)
			{
				listBoxSourceFolders.Items.RemoveAt(index);
			}
		}

		private void buttonDestinationFolderBrowse_Click(object sender, RoutedEventArgs e)
		{
			System.Windows.Forms.FolderBrowserDialog browse = new System.Windows.Forms.FolderBrowserDialog();
			browse.ShowNewFolderButton = true;
			browse.Description = "Please select a folder";
			if (browse.ShowDialog() == System.Windows.Forms.DialogResult.OK)
			{
				textBoxDestinationFolder.Text = browse.SelectedPath;
			}
		}

		private void buttonFolderFilterAdd_Click(object sender, RoutedEventArgs e)
		{
			FilterWindow filterWindow = new FilterWindow(true);

			if ((bool)filterWindow.ShowDialog())
			{
				listBoxFolderFilters.Items.Add(filterWindow.Filter);
			}
		}

		private void buttonFolderFilterDelete_Click(object sender, RoutedEventArgs e)
		{
			int index = listBoxFolderFilters.SelectedIndex;

			if (0 <= index)
			{
				listBoxFolderFilters.Items.RemoveAt(index);
			}
		}

		private void buttonFileFilterAdd_Click(object sender, RoutedEventArgs e)
		{
			FilterWindow filterWindow = new FilterWindow(false);

			if ((bool)filterWindow.ShowDialog())
			{
				listBoxFileFilters.Items.Add(filterWindow.Filter);
			}
		}

		private void buttonFileFilterDelete_Click(object sender, RoutedEventArgs e)
		{
			int index = listBoxFileFilters.SelectedIndex;

			if (0 <= index)
			{
				listBoxFileFilters.Items.RemoveAt(index);
			}
		}

		private void Window_Loaded(object sender, RoutedEventArgs e)
		{
			// Fill data.
			textBoxName.Text = project_.Name;

			foreach (String sourceFolder in project_.SourceFolders)
			{
				listBoxSourceFolders.Items.Add(sourceFolder);
			}

			textBoxDestinationFolder.Text = project_.DestinationFolder;

			foreach (String folderFilter in project_.FolderFilters)
			{
				listBoxFolderFilters.Items.Add(folderFilter);
			}

			foreach (String fileFilter in project_.FileFilters)
			{
				listBoxFileFilters.Items.Add(fileFilter);
			}

			textBoxName.Focus();
		}
	}
}
